package org.lq.his.controller;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.lq.his.dao.RoleDao;
import org.lq.his.pojo.Role;
import org.lq.his.service.PermissionService;
import org.lq.his.service.RoleService;
import org.springframework.expression.spel.ast.Assign;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {
	
	@Resource
	private RoleService roleService;

	@Resource
	private PermissionService permissionService;
	
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}


	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}


	@RequestMapping("/index")
	public String index()throws Exception{
		return "role/roleIndex";
	}
	
	@RequestMapping("/toAssign")
	public String toAssign(Integer rid,ModelMap modelMap) throws Exception{
		modelMap.put("roleId", rid);
		return "role/assign";
	}
	
	@RequestMapping("/assign")
	@ResponseBody
	public Map<String, Object> assign(Integer roleId,Integer[] ids) throws Exception{
		Map<String, Object> map = new HashMap<>();
		permissionService.deletePermissionsByRoleId(roleId);
		permissionService.addRolePermissions(roleId, Arrays.asList(ids));
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/getPermissions")
	@ResponseBody
	public List<Integer> selectPermission(Integer roleId) throws Exception{
		
		return permissionService.getPermissionIdsByRoleId(roleId);
	}
	
	@RequestMapping(value="roleForm",method=RequestMethod.GET)
	public String roleForm()throws Exception{
		return "role/roleForm";
	}
	@RequestMapping("/selectAll")
	@ResponseBody
	public List<Role> selectAll(){
		return roleService.getAll();
	}
	
	
	@RequestMapping("/listByCondition")
	@ResponseBody
	public Map<String, Object> listByCondition(Integer page,Integer rows,@RequestParam(defaultValue="id")String sort,@RequestParam(defaultValue="asc")String order,Role condition)throws Exception{
		
		Map<String, Object> map = new HashMap<>();
		
		int start = (page-1)*rows;
		
		List<Role> list = roleService.getListByCondition(start, rows, condition, sort, order);
		int total = roleService.getCountByCondition(condition);
		map.put("rows", list);
		map.put("total", total);
		return map;
		
	}
	
	@RequestMapping("/selectRoleById")
	@ResponseBody
	public Role selectRoleById(Integer id)throws Exception{
		return roleService.getRoleById(id);
	}
	
	@RequestMapping("/addRole")
	@ResponseBody
	public Map<String, Object> add(Role role)throws Exception{
		
		roleService.addRole(role);
		Map<String, Object> map = new HashMap<>();
		map.put("result", "true");
		
		return map;
	}
	@RequestMapping("/deleteRole")
	@ResponseBody
	public Map<String, Object> delete(Integer[] ids)throws Exception{
		roleService.deleteRole(ids);
		Map<String, Object> map = new HashMap<>();
		map.put("result", true);
		return map;
	}
	@RequestMapping("/editRole")
	@ResponseBody
	public Map<String, Object> editRole(Role role)throws Exception{

		roleService.editRole(role);
		Map<String, Object> map = new HashMap<>();
		map.put("result", true);
		return map;
		
	}
	
	
	
}
